package com.yzh.pinboard.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.WindowManager;

import com.yzh.pinboard.PhotoPicker.ImageFolderModel;
import com.yzh.pinboard.PhotoPicker.LoadPhotoTask;
import com.yzh.pinboard.PhotoPicker.MyAsyncTask;
import com.yzh.pinboard.R;
import com.yzh.pinboard.adapter.PhotoAdapter;
import com.yzh.pinboard.utils.DensityUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/3/8.
 */

public class PhotoPickerDialog extends Dialog implements MyAsyncTask
        .Callback<ArrayList<ImageFolderModel>> {
    private RecyclerView mPhotoPicker;
    private LoadPhotoTask mLoadPhotoTask;
    private Context mContext;
    private PhotoAdapter mPhotoAdapter;

    public PhotoPickerDialog(@NonNull Context context) {
        super(context, R.style.Translucent_NoTitle);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_photo_picker);
        mPhotoPicker = (RecyclerView) findViewById(R.id.rv_photo_picker);
        mLoadPhotoTask = new LoadPhotoTask(this, mContext).perform();


    }

    @Override
    public void show() {
        super.show();
        try {
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.width = DensityUtil.getScreenWidth(mContext);
            params.height = DensityUtil.getScreenHeight(mContext) - DensityUtil
                    .getStatusBarHeight(mContext);
            getWindow().setAttributes(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPostExecute(ArrayList<ImageFolderModel> imageFolderModels) {
        mPhotoAdapter = new PhotoAdapter(mContext, imageFolderModels.get(0).getImages
                ());
        mPhotoPicker.setLayoutManager(new GridLayoutManager(mContext, 3));
        mPhotoPicker.setAdapter(mPhotoAdapter);

        mPhotoAdapter.setOnItemClickListener(new PhotoAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position, String path) {
                if (mOnPicSelectListener != null) {
                    mOnPicSelectListener.onSelectPic(position, path);
                }
            }

        });
    }

    @Override
    public void onTaskCancelled() {
        mLoadPhotoTask = null;

    }

    @Override
    public void dismiss() {
        super.dismiss();
        cancelLoadPhotoTask();
    }

    private void cancelLoadPhotoTask() {
        if (mLoadPhotoTask != null) {
            mLoadPhotoTask.cancelTask();
            mLoadPhotoTask = null;
        }
    }

    public void setOnPicSelectListener(onPicSelectListener onPicSelectListener) {
        mOnPicSelectListener = onPicSelectListener;
    }

    private onPicSelectListener mOnPicSelectListener;

    public interface onPicSelectListener {
        void onSelectPic(int position, String path);
    }
}
